package com.example.ynanchao.testrecyclerscrollorientation;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

/**
 * Created by YnanChao on 2017/6/26.
 */

public class CCRecyclerView extends RecyclerView {
    private boolean isScrollUp;
    private int scrollY;
    private int scrollState;

    public CCRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                scrollState = newState;
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                scrollY += dy;
                boolean scrollUp = dy > 0;
                if (isScrollUp != scrollUp) {
                    isScrollUp = scrollUp;
                    if (onScrollChangedListener != null) {
                        onScrollChangedListener.onScrollOriChanged(isScrollUp);
                    }
                }

                if (onScrollChangedListener != null) {
                    onScrollChangedListener.onScrolled(recyclerView, scrollY, scrollState, isScrollUp);
                }
            }
        });
    }

    OnScrollChangedListener onScrollChangedListener;

    public void setOnScrollChangedListener(OnScrollChangedListener onScrollChangedListener) {
        this.onScrollChangedListener = onScrollChangedListener;
    }

    public interface OnScrollChangedListener {
        void onScrolled(RecyclerView recyclerView, int scrollY, int scrollState, boolean isScrollUp);

        void onScrollOriChanged(boolean isScrollUp);

    }
}
